package cn.com.thingo.hyintelligent.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.th.opensdk.openapi.AirConditionInfo;
import com.th.opensdk.openapi.CurtainInfo;
import com.th.opensdk.openapi.DimmerInfo;
import com.th.opensdk.openapi.RemoterInfo;
import com.th.opensdk.openapi.SceneInfo;
import com.th.opensdk.openapi.SwitchInfo;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.thingo.hyintelligent.R;
import cn.com.thingo.hyintelligent.ui.activity.AirConditionControlActivity;
import cn.com.thingo.hyintelligent.ui.activity.AirConditionControlImportActivity;
import cn.com.thingo.hyintelligent.ui.adapter.decoration.RecycleViewDivider;

public class DeviceListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private LiteListAdapter liteAdapter;
    private WindowListAdapter windowAdapter;
    private CurtainListAdapter curtainAdapter;
    private AirConditionListAdapter airConditionAdapter;
    //    private TelevisionListAdapter televisionAdapter;
    private DoorLockListAdapter doorLockAdapter;
    private MusicListAdapter musicAdapter;
    private List<RemoterInfo> airConditionList;
    private List<AirConditionInfo> airConditionInfos;


    private List<Integer> deviceList;
    public static int DEVICE_TYPE_LITE = 1;
    public static int DEVICE_TYPE_WINDOW = 2;
    public static int DEVICE_TYPE_CURTAIN = 3;
    public static int DEVICE_TYPE_AIR_CONDITION = 4;
    public static int DEVICE_TYPE_TELEVISION = 5;
    public static int DEVICE_TYPE_DOOR_LOCK = 6;
    public static int DEVICE_TYPE_MUSIC = 7;
    private LayoutInflater inflater;


    public DeviceListAdapter(Context context, List<DimmerInfo> dimmerInfoList, List<SwitchInfo> switchInfoList,
                             List<SceneInfo> sceneInfoList, List<CurtainInfo> curtainInfoList, List<RemoterInfo> airConditionList, List<AirConditionInfo> airConditionInfos,
                             List<RemoterInfo> televisionList, List<RemoterInfo> doorLockList, List<RemoterInfo> musicList, List<Integer> deviceList) {
        this.context = context;
        this.deviceList = deviceList;
        this.airConditionList = airConditionList;
        this.airConditionInfos = airConditionInfos;
        this.inflater = LayoutInflater.from(context);
        liteAdapter = new LiteListAdapter(switchInfoList, dimmerInfoList, context);
        windowAdapter = new WindowListAdapter(sceneInfoList, context);
        curtainAdapter = new CurtainListAdapter(curtainInfoList, context);
        airConditionAdapter = new AirConditionListAdapter(airConditionList, airConditionInfos, context);
//        televisionAdapter = new TelevisionListAdapter(televisionList, context);
        doorLockAdapter = new DoorLockListAdapter(doorLockList, context);
        musicAdapter = new MusicListAdapter(musicList, context);

    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == DEVICE_TYPE_LITE) {
            return new LiteViewHolder(inflater.inflate(R.layout.device_item_lite, parent, false));
        } else if (viewType == DEVICE_TYPE_WINDOW) {
            return new WindowViewHolder(inflater.inflate(R.layout.device_item_window, parent, false));
        } else if (viewType == DEVICE_TYPE_CURTAIN) {
            return new CurtainViewHolder(inflater.inflate(R.layout.device_item_curtain, parent, false));
        } else if (viewType == DEVICE_TYPE_AIR_CONDITION) {
            return new AirConditionViewHolder(inflater.inflate(R.layout.device_item_air_condition, parent, false));
        } else if (viewType == DEVICE_TYPE_TELEVISION) {
            return new TelevisionViewHolder(inflater.inflate(R.layout.device_item_television, parent, false));
        } else if (viewType == DEVICE_TYPE_DOOR_LOCK) {
            return new DoorLockViewHolder(inflater.inflate(R.layout.device_item_door_lock, parent, false));
        } else {
            return new MusicViewHolder(inflater.inflate(R.layout.device_item_music, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int viewType = getItemViewType(position);
        if (viewType == DEVICE_TYPE_LITE) {
            ((LiteViewHolder) holder).rvLite.setLayoutManager(new LinearLayoutManager(context));
            ((LiteViewHolder) holder).rvLite.addItemDecoration(new RecycleViewDivider(context, LinearLayoutManager.VERTICAL, 1, context.getResources().getColor(R.color.gray_12)));
            ((LiteViewHolder) holder).rvLite.setAdapter(liteAdapter);
            liteAdapter.notifyDataSetChanged();
        } else if (viewType == DEVICE_TYPE_WINDOW) {
            ((WindowViewHolder) holder).rvWindow.setLayoutManager(new LinearLayoutManager(context));
            ((WindowViewHolder) holder).rvWindow.addItemDecoration(new RecycleViewDivider(context, LinearLayoutManager.VERTICAL, 1, context.getResources().getColor(R.color.gray_12)));
            windowAdapter.setCallBack((WindowListAdapter.CallBack) context);
            ((WindowViewHolder) holder).rvWindow.setAdapter(windowAdapter);
            windowAdapter.notifyDataSetChanged();
        } else if (viewType == DEVICE_TYPE_CURTAIN) {
            ((CurtainViewHolder) holder).rvCurtain.setLayoutManager(new LinearLayoutManager(context));
            ((CurtainViewHolder) holder).rvCurtain.addItemDecoration(new RecycleViewDivider(context, LinearLayoutManager.VERTICAL, 1, context.getResources().getColor(R.color.gray_12)));
            ((CurtainViewHolder) holder).rvCurtain.setAdapter(curtainAdapter);
            curtainAdapter.notifyDataSetChanged();
        } else if (viewType == DEVICE_TYPE_AIR_CONDITION) {
            ((AirConditionViewHolder) holder).rvAirCondition.setLayoutManager(new LinearLayoutManager(context));
            ((AirConditionViewHolder) holder).rvAirCondition.addItemDecoration(new RecycleViewDivider(context, LinearLayoutManager.VERTICAL, 1, context.getResources().getColor(R.color.gray_12)));
            airConditionAdapter.setListener(new AirConditionListAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(int position, int type) {
                    if (type == AirConditionListAdapter.AIRCONDITION_CUSTOM) {
                        RemoterInfo remoterInfo = airConditionList.get(position);
                        long remoterId = remoterInfo.getRemoterId();
                        Intent intent = new Intent(context, AirConditionControlActivity.class);
                        intent.putExtra("remoter_id", remoterId);
                        context.startActivity(intent);
                    } else {
                        Intent intent = new Intent(context, AirConditionControlImportActivity.class);
                        intent.putExtra("air_condition_position", position);
                        intent.putExtra("current_room_id",airConditionInfos.get(position).getRoomId());
                        context.startActivity(intent);
                    }

                }
            });
            ((AirConditionViewHolder) holder).rvAirCondition.setAdapter(airConditionAdapter);
            airConditionAdapter.notifyDataSetChanged();
        } else if (viewType == DEVICE_TYPE_TELEVISION) {
        } else if (viewType == DEVICE_TYPE_DOOR_LOCK) {
            ((DoorLockViewHolder) holder).rvDoorLock.setLayoutManager(new LinearLayoutManager(context));
            ((DoorLockViewHolder) holder).rvDoorLock.addItemDecoration(new RecycleViewDivider(context, LinearLayoutManager.VERTICAL, 1, context.getResources().getColor(R.color.gray_12)));
            ((DoorLockViewHolder) holder).rvDoorLock.setAdapter(doorLockAdapter);
            doorLockAdapter.notifyDataSetChanged();
        } else {
            ((MusicViewHolder) holder).rvMusic.setLayoutManager(new LinearLayoutManager(context));
            ((MusicViewHolder) holder).rvMusic.addItemDecoration(new RecycleViewDivider(context, LinearLayoutManager.VERTICAL, 1, context.getResources().getColor(R.color.gray_12)));
            ((MusicViewHolder) holder).rvMusic.setAdapter(musicAdapter);
            musicAdapter.notifyDataSetChanged();
        }

    }

    @Override
    public int getItemCount() {
        return deviceList.size();
    }

    @Override
    public int getItemViewType(int position) {
        return deviceList.get(position);
    }


    static class LiteViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.device_item_lite_list)
        RecyclerView rvLite;

        LiteViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    static class WindowViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.device_item_window_list)
        RecyclerView rvWindow;

        WindowViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    static class CurtainViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.device_item_curtain_list)
        RecyclerView rvCurtain;

        CurtainViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    static class AirConditionViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.device_item_air_condition_list)
        RecyclerView rvAirCondition;

        AirConditionViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    static class TelevisionViewHolder extends RecyclerView.ViewHolder {
        //todo 待对接
        TelevisionViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    static class DoorLockViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.device_item_door_lock_list)
        RecyclerView rvDoorLock;

        DoorLockViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    static class MusicViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.device_item_music_list)
        RecyclerView rvMusic;

        MusicViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
